package ar.com.android.alco.adapters;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import ar.com.android.alco.forms.R;

public class CalendarioGridCellAdapter extends BaseAdapter {
	private final Context _context;
	Activity activity;

	private final List<String> list;
	private static final int DAY_OFFSET = 1;
	private final int[] daysOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	private final int month, year;
	private int daysInMonth;
	private int currentDayOfMonth;
	private String currentDate;
	private int currentWeekDay;
	private Button gridcell;

	// Days in Current Month
	public CalendarioGridCellAdapter(Activity activity, int month, int year)
	{
		super();
		this.activity = activity;
		this._context = activity.getApplicationContext();
		this.list = new ArrayList<String>();
		this.month = ++month;
		this.year = year;
		
		DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

		Calendar calendar = Calendar.getInstance();
		setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
		setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));
		setCurrentDate(dateFormat.format(calendar.getTime()));

		// Print Month
		printMonth(month, year);
	}

	private int getNumberOfDaysOfMonth(int i)
	{
		return daysOfMonth[i];
	}

	public String getItem(int position)
	{
		return list.get(position);
	}

	public int getCount()
	{
		return list.size();
	}

	private void printMonth(int mm, int yy)
	{
		int trailingSpaces = 0;
		int daysInPrevMonth = 0;
		int prevMonth = 0;
		int prevYear = 0;
		int nextMonth = 0;
		int nextYear = 0;
		
		DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

		int currentMonth = mm - 1;
		daysInMonth = getNumberOfDaysOfMonth(currentMonth);

		// Gregorian Calendar : MINUS 1, set to FIRST OF MONTH
		GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);

		if (currentMonth == 11)
		{
			prevMonth = currentMonth - 1;
			daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
			nextMonth = 0;
			prevYear = yy;
			nextYear = yy + 1;
		}
		else if (currentMonth == 0)
		{
			prevMonth = 11;
			prevYear = yy - 1;
			nextYear = yy;
			daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
			nextMonth = 1;
		}
		else
		{
			prevMonth = currentMonth - 1;
			nextMonth = currentMonth + 1;
			nextYear = yy;
			prevYear = yy;
			daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
		}

		// Compute how much to leave before before the first day of the
		// month.
		// getDay() returns 0 for Sunday.
		int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
		trailingSpaces = currentWeekDay;

		if (cal.isLeapYear(cal.get(Calendar.YEAR)) && mm == 2)
			++daysInMonth;
		
		for (int i = 0; i < trailingSpaces; i++)
			list.add(String.valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET) + i) + "-PREVMES" + "-" + prevMonth + "-" + prevYear);

		// Current Month Days
		for (int i = 1; i <= daysInMonth; i++)
		{
			String procDate = dateFormat.format(new Date(yy - 1900, currentMonth, i));
			if (procDate.equals(getCurrentDate()))
				list.add(String.valueOf(i) + "-DIAACTUAL" + "-" + currentMonth + "-" + yy);
			else
				list.add(String.valueOf(i) + "-DIAMES" + "-" + currentMonth + "-" + yy);
		}
		// Leading Month days
		for (int i = 0; i < list.size() % 7; i++)
			list.add(String.valueOf(i + 1) + "-SIGMES" + "-" + nextMonth + "-" + nextYear);
	}

	public long getItemId(int position)
	{
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent)
	{
		View row = convertView;
		if (row == null)
		{
			LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.drawable.calendar_day_gridcell, parent, false);
		}
		
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

		// Get a reference to the Day gridcell
		gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);
		gridcell.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				if(v.getTag().equals("-"))
					return;
				TextView tvValFecha = (TextView) activity.findViewById(R.id.tvValFecha);
				tvValFecha.setText(v.getTag().toString());
				gridcell.setSelected(true);
			}
		});

		String[] dayParam = list.get(position).split("-");
		String theday = dayParam[0];
		String themonth = dayParam[2];
		String theyear = dayParam[3];

		gridcell.setTag(dateFormat.format(new Date(Integer.parseInt(theyear) - 1900, Integer.parseInt(themonth), Integer.parseInt(theday))));

		if (dayParam[1].equals("DIAMES")) {
			gridcell.setTextColor(Color.WHITE);
			gridcell.setText(theday);
		}

		if (dayParam[1].equals("DIAACTUAL")) {
			gridcell.setTextColor(Color.RED);
			gridcell.setText(theday);
		}
		
		if (dayParam[1].equals("PREVMES") || dayParam[1].equals("SIGMES")) {
			gridcell.setTextColor(Color.WHITE);
			gridcell.setText("-");
			gridcell.setTag("-");
		}
		return row;
	}

	public int getCurrentDayOfMonth()
	{
		return currentDayOfMonth;
	}

	private void setCurrentDayOfMonth(int currentDayOfMonth)
	{
		this.currentDayOfMonth = currentDayOfMonth;
	}

	public void setCurrentWeekDay(int currentWeekDay)
	{
		this.currentWeekDay = currentWeekDay;
	}

	public int getCurrentWeekDay()
	{
		return currentWeekDay;
	}
	public int getMonth() {
		return month;
	}
	public int getYear() {
		return year;
	}

	public String getCurrentDate() {
		return currentDate;
	}

	public void setCurrentDate(String currentDate) {
		this.currentDate = currentDate;
	}
}
